package creator.prototype;

import java.io.*;

public class DeepClone implements Cloneable, Serializable {

    private String name;
    private Integer age;
    public QianClone qianClone;


    public DeepClone() {

    }

    public DeepClone(String name, Integer age) {
        this.name = name;
        this.age = age;

    }

    // 方式一:  clone
    @Override
    public Object clone() throws CloneNotSupportedException {
        // 将基础属性和String进行拷贝
        DeepClone clone = (DeepClone) super.clone();
        clone.qianClone = (QianClone) qianClone.clone();
        return clone;
    }

    // 方式二: 序列化
    public DeepClone serizableClone() {
        ByteArrayInputStream bis = null;
        ByteArrayOutputStream bos = null;
        ObjectInputStream ois = null;
        ObjectOutputStream oos = null;

        try {
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            DeepClone o = (DeepClone) ois.readObject();
            return o;
        } catch (Exception e) {
            System.out.println("e = " + e);
        } finally {

        }
        return null;
    }

    @Override
    public String toString() {
        return "QianClone{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", deepClone.hashcode=" + qianClone.hashCode() +
                '}';
    }


}
